/*!
 @name：listview
 @date: 2022.07.13
 @update:yuguoliang
 @blog: http://www.web000.cn/
 */

;(function (window, $, undefined) {
    var laytpl = window.laytpl;

    //构造函数
    function Listview(ele, opts) {
        var _self = this;
        _self.ele = ele;
        _self.opts = opts;
        _self.init();
    }

    Listview.prototype.init = function () {
        var _self = this;
        //处理摸版标签
        _self.tpl = _self.opts.tpl ? _self.opts.tpl : $(_self.ele).find('template').html();
        _self.opts.empty ? $(_self.ele).empty2(true) : '';
        var _listcontainer = $(_self.ele).find('.' + _self.opts.classWrapper);
        if (_listcontainer.length > 0) {
            _self.listcontainer = _listcontainer[0];
        } else {
            var listcontainer = document.createElement('div');
            listcontainer.className = _self.opts.classWrapper;
            _self.ele.appendChild(listcontainer);
            _self.listcontainer = listcontainer;
        }

        //处理数据
        if (_self.opts.url) {
            //ajax
            _self.loaddata();

            //滚动分页加载
            if (_self.opts.isScrollPage) {
                var _scrollDom = _self.opts.scrollDom ? _self.opts.scrollDom : _self.ele;
                $(_scrollDom).infinite().on("infinite", function () {
                    _self.loaddata();
                });
            }
        } else {
            if (!_self.opts.data) return false;
            var dataList = _self.opts.data;
            if (_self.opts.keyEach && $.isObject(_self.opts.data) && _self.opts.data[_self.opts.keyEach]) {
                dataList = _self.opts.data[_self.opts.keyEach];
            }
            _self.dataList = $.isArray(dataList) ? dataList : [dataList];
            _self.filldata();
            if ($.isFunction(_self.opts.loadsuccess)) {
                _self.opts.loadsuccess.call(_self.listcontainer, _self.dataList);
            }
        }
    };

    Listview.prototype.loaddata = function () {
        var _self = this;
        if (_self.loading) return;
        _self.loading = true;
        $.ajax({
            'type': _self.opts.extraParams.type,
            'url': _self.opts.url,
            'data': _self.opts.extraParams.data,
            'success': function (data) {
                if (data.ResultCode == "0") {
                    var dataList = data;
                    if (_self.opts.keyEach && $.isObject(data) && data[_self.opts.keyEach]) {
                        dataList = data[_self.opts.keyEach];
                    }
                    _self.dataList = $.isArray(dataList) ? dataList : [dataList];
                    _self.filldata();

                    //loadsuccess回调函数
                    if ($.isFunction(_self.opts.loadsuccess)) {
                        _self.opts.loadsuccess.call(_self.listcontainer, _self.dataList);
                    }

                    //滚动分页加载
                    if (_self.opts.isScrollPage) {
                        var totalList = data.Total //总记录数
                        var totalPage = Math.ceil(totalList / _self.opts.extraParams.data.pageSize);
                        var _scrollDom = _self.opts.scrollDom ? _self.opts.scrollDom : _self.ele;
                        if (totalPage > _self.opts.extraParams.data.pageIndex + 1) {
                            $(".weui-loadmore").removeClass("hide");
                        } else {
                            $(_scrollDom).destroyInfinite();
                            $(".weui-loadmore").addClass("hide");
                        }
                        _self.opts.extraParams.data.pageIndex++;
                    }

                } else {
                    console.log(data.ErrMsg);
                }
            },
            'error': function () {
            },
            'complete': function () {
                _self.loading = false;
            }
        })
    };

    Listview.prototype.filldata = function () {
        var _self = this;
        var _data = _self.dataList;
        var keyarr = [];
        for (var key in _data[0]) {
            keyarr.push(key);
        }
        for (var i = 0; i < _data.length; i++) {
            _self.addlistitem(keyarr, _data[i], i);
        }
    };

    Listview.prototype.addlistitem = function (keyarr, dataitem, i) {
        var _self = this;
        dataitem.rowIndex = i;
        var _tpl = laytpl(_self.tpl).render(dataitem);
        var listitem = document.createElement('div');
        listitem.className = _self.opts.classItem;
        listitem.innerHTML = _tpl;
        if (_self.opts.itemTap) {
            $(listitem).on('click', function (event) {
                _self.opts.itemTap.call(this, listitem, dataitem, _self.dataList, event);
            });
        }
        _self.listcontainer.appendChild(listitem);
        return true;
    };

    $.fn.listview = function (setting) {
        var defaults = {
            "tpl": null,
            "url": null,
            "extraParams": {
                "type": "get",
                "data": {
                    "pageIndex": 1,
                    "pageSize": 11,
                }
            },
            "isScrollPage": false,
            "scrollDom": 'body',
            "data": null,
            "keyEach": null,
            "classWrapper": "listviews",
            "classItem": "item-listview",
            "itemTap": null,
            "empty": false,
            "loadsuccess": null,
        };
        var options = $.extend({}, defaults, setting);
        return this.each(function () {
            var el = new Listview(this, options);
        })
    }
})(window, Dom7);
